package com.sgb.kjwl.view.ui.activity.enterprise;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddnewneedAssetsBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;

import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.sgb.kjwl.model.entity.enterprise.OldAndNewTypeEntiy;
import com.sgb.kjwl.pop.ChoiceMorePop;
import com.sgb.kjwl.viewmodel.enterprise.AddNeed_AssetsModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.InputFilterUtils;
import com.swgk.core.dialog.DialogHelper;

import java.io.Serializable;

import javax.inject.Inject;

/**
 * 发布找资产
 */
public class AddNeed_AssetsActivity extends AppActivity {
    @Inject
    AddNeed_AssetsModel mModel;
    private ActivityAddnewneedAssetsBinding mBinding;
    private OptionsPickerView optionsPickerView_assets_type;
    private Dialog choicemore;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }
    @Override
    protected void initView() {
        super.initView();
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_addnewneed_assets);
        mBinding.setVariable(BR.mAssetsmodel,mModel);
        mBinding.setLifecycleOwner(this);

        InputFilterUtils.setEditTextInhibitInputSpaChat(mBinding.tvName,15);
        InputFilterUtils.setEditTextInhibitInputSpaChat(mBinding.tvinputtitle,50);
        mBinding.ediMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence editable, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();
                mBinding.ediMoney.removeTextChangedListener(this);

                if (s.contains(".")) {
                    mBinding.ediMoney.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10 + 3 + 1)});
                    if (s.length() - 1 - s.indexOf(".") > 2) {
                        s = s.substring(0,
                                s.indexOf(".") + 2 + 1);
                        editable.replace(0, editable.length(), s.trim());
                    }
                } else {
                    mBinding.ediMoney.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10 + 1)});
                    if (s.length() > 10) {
                        s = s.substring(0, 10);
                        editable.replace(0, editable.length(), s.trim());
                    }
                }
                if (s.trim().equals(".")) {
                    s = "0" + s;
                    editable.replace(0, editable.length(), s.trim());
                }

                if (s.startsWith("0") && s.trim().length() > 1) {
                    if (!s.substring(1, 2).equals(".")) {
                        editable.replace(0, editable.length(), "0");
                    }
                }
                mBinding.ediMoney.addTextChangedListener(this);
            }
        });
        mModel.name.observe(this,String ->{
            if(String.indexOf(" ")>-1) {
                mModel.name.postValue(String.trim());
            }
        });
        mModel.user.observe(this,String ->{
            if(String.indexOf(" ")>-1) {
                mModel.user.postValue(String.trim());
            }
        });

        if(getIntent().getSerializableExtra("data")!=null){
            mModel.data=(NeedListItemEntity)getIntent().getSerializableExtra("data");
            mModel.setdata();

        }
        mModel.toast_message.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtil.show(AddNeed_AssetsActivity.this,s);
            }
        });
        mModel.yijia.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    mModel.money.postValue("");
                    mBinding.ediMoney.setEnabled(false);
                }else{
                    mBinding.ediMoney.setEnabled(true);
                }
            }
        });
        mModel.money.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if(!TextUtils.isEmpty(s)){
                    mModel.yijia.postValue(false);
                }
            }
        });


        mModel.showchoicepopwindow.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer){
                    case 1:
                        if(optionsPickerView_assets_type==null){
                            optionsPickerView_assets_type = new OptionsPickerBuilder(AddNeed_AssetsActivity.this, new OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                    mModel.assets_type.postValue(mModel.listdata.get(options1).getClassificationName());
                                    mModel.assets_type_id = mModel.listdata.get(options1).getClassificationType()+"";

                                    if(mModel.listdata.get(options1).getRelateOld()==0){
                                        mModel.ishow.postValue(true);
                                        mModel.ids = "";
                                        mModel.newandoldtype.postValue("");
                                    }else{
                                        mModel.ishow.postValue(false);
                                        mModel.ids = "";
                                        mModel.newandoldtype.postValue("");
                                    }
                                }
                            }).setCancelText("取消").setSubCalSize(15)
                                    .setCancelColor(Color.parseColor("#666666"))
                                    .setTitleText("选择类型")
                                    .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                                    .setSubmitText("确定")
                                    .setSubmitColor(Color.parseColor("#5792FD"))
                                    .setTextColorCenter(Color.parseColor("#ff333333"))
                                    .setContentTextSize(15)
                                    .setLineSpacingMultiplier(2.5f)
                                    .setDividerColor(Color.parseColor("#EDEDED"))
                                    .setDecorView(getWindow().getDecorView().findViewById(android.R.id.content)).build();
                            optionsPickerView_assets_type.setPicker(mModel.listdata);
                            optionsPickerView_assets_type.setSelectOptions(0);
                        }
                        optionsPickerView_assets_type.show();
                        break;
                    case 2:
                        if(choicemore==null){
                            choicemore = new ChoiceMorePop.Builder<OldAndNewTypeEntiy>(getActivity()).setTitle("选择类型").setListData(mModel.oldandnewtype_list).setListOnclickLister(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String choicedata = "";
                                    mModel.ids = "";
                                    for (OldAndNewTypeEntiy listdatum : mModel.oldandnewtype_list) {
                                        if(listdatum.getChoice()){
                                            choicedata = choicedata+listdatum.getName()+",";
                                            mModel.ids = mModel.ids+listdatum.getId()+",";
                                        }
                                    }
                                    if(!TextUtils.isEmpty(choicedata)){
                                        choicedata = choicedata.substring(0,choicedata.length()-1);
                                        mModel.newandoldtype.postValue(choicedata);
                                    }
                                    if(!TextUtils.isEmpty(mModel.ids)){
                                        mModel.ids = mModel.ids.substring(0,mModel.ids.length()-1);
                                    }


                                }
                            }).create();
                        }

                        choicemore.show();
                        break;
                }
            }
        });
        mModel.showloading_diag.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                switch (integer){
                    case 1:
                        DialogHelper.showProgressDialog(AddNeed_AssetsActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                        break;
                    case 2:
                        DialogHelper.dismissProgressDialog();
                        break;

                    case 3://提交成功
                        DialogHelper.dismissProgressDialog();
//                        if(mModel.data!=null){
//                            Intent mintent = new Intent();
//                            mintent.putExtra("data",(Serializable)mModel.data);
//                            setResult(AddNeedDetailActivity.REQUEST_CODE,mintent);
//                        }
                        AddNeedListActivity.startactivity(AddNeed_AssetsActivity.this);
                        finish();
                        break;
                }
            }
        });
    }

    public static void startactivity(Context mContext){
        Intent mIntent = new Intent();
        mIntent.setClass(mContext,AddNeed_AssetsActivity.class);
        mContext.startActivity(mIntent);
    }
}
